package com.yc.bdw.Activity;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yc.bdw.Adapter.SortListAdapter;
import com.yc.bdw.Fragment.SortListFragment;
import com.yc.bdw.R;
import com.yc.bdw.Utils.Constant;


public class SortListActivity extends FragmentActivity implements
		OnItemClickListener {

	private String[] str_sortlist = Constant.SORT_LIST;
	private ListView listView;
	private SortListAdapter sortListAdapter;
	private SortListFragment sortListFragment;
	public static int mPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sortlist);

		initView();
	}

	private void initView() {
		listView = (ListView) findViewById(R.id.listview);

		sortListAdapter = new SortListAdapter(this, str_sortlist);
		listView.setAdapter(sortListAdapter);
		listView.setOnItemClickListener(this);

		//创建MyFragment对象
		sortListFragment = new SortListFragment();
		FragmentTransaction fragmentTransaction = getSupportFragmentManager()
				.beginTransaction();
		fragmentTransaction.replace(R.id.fragment_container, sortListFragment);
		//通过bundle传值给MyFragment
		Bundle bundle = new Bundle();
		bundle.putString(SortListFragment.TAG, str_sortlist[mPosition]);
		sortListFragment.setArguments(bundle);
		fragmentTransaction.commit();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		//拿到当前位置
		mPosition = position;
		//即时刷新adapter
		sortListAdapter.notifyDataSetChanged();
		for (int i = 0; i < str_sortlist.length; i++) {
			sortListFragment = new SortListFragment();
			FragmentTransaction fragmentTransaction = getSupportFragmentManager()
					.beginTransaction();
			fragmentTransaction.replace(R.id.fragment_container, sortListFragment);
			Bundle bundle = new Bundle();
			bundle.putString(SortListFragment.TAG, str_sortlist[position]);
			sortListFragment.setArguments(bundle);
			fragmentTransaction.commit();
		}
	}
}
